package com.thunder.soft.ui.activity;

import android.content.Intent;
import android.widget.Toast;

import androidx.lifecycle.Observer;

import com.thunder.soft.R;
import com.thunder.soft.bean.UserBean;
import com.thunder.soft.common.base.BaseActivity;
import com.thunder.soft.common.util.ViewModelProviderHelper;
import com.thunder.soft.databinding.ActivityRegisterBinding;
import com.thunder.soft.vm.RegisterViewModel;

public class RegisterActivity extends BaseActivity<ActivityRegisterBinding> {

    public RegisterViewModel registerViewModel;

    @Override
    protected void initData() {
        registerViewModel = ViewModelProviderHelper.buildViewModel(this, RegisterViewModel.class);
        getLifecycle().addObserver(registerViewModel);
        binding.setRegisterViewModel(registerViewModel);

        registerViewModel.getErrorMsg().observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                Toast.makeText(RegisterActivity.this, s, Toast.LENGTH_SHORT).show();
            }
        });

        registerViewModel.getLiveData().observe(this, new Observer<UserBean>() {
            @Override
            public void onChanged(UserBean userBeans) {
                startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
                Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
            }
        });

        registerViewModel.getFinishActivity().observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                finish();
            }
        });
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_register;
    }
}